<?php
	if(!defined('IN_SYS'))die('Hacking attempt');
	class DbOperate {
		public $db;
		public $tbprefix = '';
		private $debug = false;

		public function __construct() {
			global $db,$tbprefix;
			$this ->db = $db;
			$this -> tbprefix = $tbprefix;
		}
		public function getAllTabName() {
			$sql = "show tables";
			$arr = $this->db->fetch($sql);
			$tab_arr = array();
			for($i=0,$max=intval(count($arr));$i<$max;$i++) {
					$tmp = array_values($arr[$i]);
					$tab_arr[] = $tmp[0];
			}
			return $tab_arr;
		}
		function bakupData($tb_arr,$start = 0) {
			global $sizelimit,$startfrom,$tbid,$rows,$stop;
			$tbid=$tbid ? $tbid-1 : 0;
			$stop = 0;
			$t_count = count($tb_arr);
			for($i=$tbid;$i<$t_count;$i++){
				$res = $this -> db -> sql_query("SHOW TABLE STATUS LIKE '$tb_arr[$i]'");
				$arr = $this -> db -> sql_fetchrowset($res);
				$rows = (int)$arr[0]['Rows'];

				$limitadd = "LIMIT $start,100000";
				$query = mysql_query("SELECT * FROM $tb_arr[$i] $limitadd");
				$num_F = mysql_num_fields($query);
				while (FALSE!=$datadb = mysql_fetch_row($query)) {
					$start++;
					$table=$tb_arr[$i];
					$bakupdata .= "INSERT INTO $table VALUES("."'".addslashes($datadb[0])."'";
					$tempdb='';
					for ($j=1;$j<$num_F;$j++) {
						$tempdb .= ",'".addslashes($datadb[$j])."'";
					}
					$bakupdata .=$tempdb. ");\n";
					if ($sizelimit && strlen($bakupdata)>$sizelimit*1000) {
						break;
					}
				}
				mysql_free_result($query);
				if($start>=$rows) {
					$start=0;
					$rows=0;
				}

				$bakupdata .="\n";
				if($sizelimit && strlen($bakupdata)>$sizelimit*1000){
					$start==0 && $i++;
					$stop=1;
					break;
				}
				$start=0;
			}
			if($stop==1) {
				$i++;
				$tbid = $i;
				$startfrom = $start;
				$start = 0;
			}
			return $bakupdata;
		}
		public function bakupTable($tb_arr) {
			foreach ($tb_arr as $table) {
				$creattable.= "DROP TABLE IF EXISTS $table;\n";
				$res = $this -> db -> sql_query("SHOW CREATE TABLE $table",ARRAY_A);
				$ctable = $this -> db -> sql_fetchrowset($res);
				$creattable .= $ctable[0]['Create Table'].";\n\n";
			}
			return $creattable;
		}
		public function bakInData($file) {
			global $dbcharset;
			$sql = file($file);
			$query='';
			$num=0;
			foreach ($sql as $key => $value) {
				$value = trim($value);
				if (!$value || $value[0]=='#') continue;
				if (eregi("\;$",$value)) {
					$query .= $value;
	
					if (eregi("^CREATE",$query)) {
						$extra   = substr(strrchr($query,')'),1);
						$tabtype = substr(strchr($extra,'='),1);
						$tabtype = substr($tabtype, 0, strpos($tabtype,strpos($tabtype,' ') ? ' ' : ';'));
						$query   = str_replace($extra,'',$query);
						if (version_compare(mysql_get_server_info(), '4.1.0', '>=')) {
							$extra = $dbcharset ? "ENGINE=$tabtype DEFAULT CHARSET=".$dbcharset.";" : "ENGINE=$tabtype;";
						} else {
							$extra = "TYPE=$tabtype;";
						}
						$query .= $extra;
					
					} elseif(eregi("^INSERT",$query)) {
						$query = 'REPLACE '.substr($query,6);
					}
					

					//mysql_query($query);
					$this -> db -> sql_query($query);
					$query='';
				} else{
					$query.=$value;
				}
			}
		}

		public function getBackupFileInfo() {
			$filedb = array();
			$handle = opendir(SYS_ROOT.'data/db_backup');
			while ($file = readdir($handle)) {
				if (eregi("^PHPWMS_",$file) && eregi("\.sql$",$file)) {
					$strlen = eregi("^PHPWMS_",$file) ? 16 + strlen("PHPWMS_") : 19;
					$fp = fopen(SYS_ROOT."data/db_backup/$file",'rb');
					$bakinfo = fread($fp,200);
					fclose($fp);
					$detail=explode("\n",$bakinfo);
					$bk['name']		= $file;
					$bk['version']	= substr($detail[1],10);
					$bk['time']		= substr($detail[2],8);
					$bk['pre']		= substr($file,0,$strlen);
					$bk['num']		= substr($file,$strlen,strrpos($file,'.')-$strlen);
					$filedb[] = $bk;
				}
			}
			return $this -> setFileInfo($filedb);
		}

		private function setFileInfo($arr) {
			$tmp1 = $tmp2 = array();

			foreach ($arr as $k => $v) {
				$tmp1[] = $v['pre'];
			}
			$dbf = array_unique($tmp1);
			sort($dbf);
			
			for($i=0,$max=intval(count($dbf));$i<$max;$i++) {
				$j = 0;
				$tmp2[$i]['name'] = $dbf[$i].'.sql';
				$tmp2[$i]['pre'] = $dbf[$i];
				foreach ($arr as $a => $b) {
					if ($j == 0) {
						$tmp2[$i]['version'] = $b['version'];
						$tmp2[$i]['time'] = $b['time'];
					}
					if ($b['pre'] == $dbf[$i]) {
						$tmp2[$i]['flist'][] = $b;
						$j += 1;
					}
				}
				$tmp2[$i]['count'] = $j;
			}
			
			return $tmp2;			
		}

		public function getSqlFile($pre) {
			$farr = $this -> getBackupFileInfo();
			
			foreach ($farr as $k => $v) {
				if ($v['pre'] == $pre) {
					return $v['flist'];
				}
			}
		}

		public function delBackup($arr) {
			if (!is_array($arr)) {
				return false;
			}
			$farr = $this -> getBackupFileInfo();
			
			foreach ($arr as $k => $v) {
				
				foreach ($farr as $a => $b) {
					if ($b['pre'] == $v) {
						
						foreach ($b['flist'] as $c => $d) {
							$file = SYS_ROOT.'data/db_backup/'.$d['name'];
							self::delSqlFile($file);
							
						}

					}					
				}
			}

		}

		private function delSqlFile($file) {
			if (!is_file($file)) {
				return false;
			}
			@chmod($file, 0777);
			return @unlink($file);
		}
		
		public function __destruct() {
			$this -> db	= null;
			$this -> tbprefix	= null;
		}
	}
?>